package CollectionDemo;

import java.util.*;

public class ColDemo1 {
    public static void main(String[] args) {
//        Collections中addAll()方法演示
        List<String> list = new ArrayList<String>();
//        添加元素
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
//        添加自身
        list.addAll(list);
//        输出
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
//        无参toArray()
        Object[] str = list.toArray();//集合转数组
        //遍历数组
        for (int i = 0; i < str.length; i++) {
            System.out.print(str[i] + " ");
        }
        System.out.println();
        print(list);


        Set<String> set = new HashSet<String>();
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
        set.add("5");
        set.add("6");
//        自身添加
        set.addAll(set);//set不可以添加自身，元素不可重复
        Iterator<String> iterator1 = list.iterator();//不可以set。iterator()
        while(iterator1.hasNext()){
            System.out.print(iterator1.next() + " ");
        }
    }
    public static void print(List<String> list){
        for (String s : list) {
            System.out.print(s + " ");
        }
        System.out.println();
    }
}
